/**
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */
  
package com.common.util;

import java.io.IOException;  
import java.io.InputStream;  
import java.io.StringWriter;
import java.util.List;  
  
import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;  

/**
 * json转换工具
 * @author limk
 * @date 2018年2月13日 上午10:04:27
 * @version 1.0
 */
public class JsonUtils {  
  
	private static ObjectMapper mapper = new ObjectMapper();
	
    /**
     * java 对象转换为 json 字符串 
     * @param obj
     * @return
     */
    public static String toJSON(Object obj) {  
        StringWriter writer = new StringWriter();  
        try {  
            mapper.writeValue(writer, obj);  
        } catch (JsonGenerationException e) {  
            throw new RuntimeException(e);  
        } catch (JsonMappingException e) {  
            throw new RuntimeException(e);  
        } catch (IOException e) {  
            throw new RuntimeException(e);  
        }  
        String result = writer.toString();  
        return (null == result) ? "" : result.replaceAll("null", "\"\"");  
    }  
  
    /**
     * json字符串转换为对象 
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T fromJSON(String json, Class<T> clazz) {  
          
        try {  
            return mapper.readValue(json, clazz);  
        } catch (JsonParseException e) {  
            throw new RuntimeException(e);  
        } catch (JsonMappingException e) {  
            throw new RuntimeException(e);  
        } catch (IOException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    /**
     * json的字节流转换为对象
     * @param json
     * @param clazz
     * @return
     */
    public static <T> T fromJSON(InputStream json, Class<T> clazz) {  
        try {  
            return mapper.readValue(json, clazz);  
        } catch (JsonParseException e) {  
            throw new RuntimeException(e);  
        } catch (JsonMappingException e) {  
            throw new RuntimeException(e);  
        } catch (IOException e) {  
            throw new RuntimeException(e);  
        }  
    }
    
    /**
     * json的字节流转换为对象 
     * @param request
     * @param clazz
     * @return
     */
    public static <T> T getRequestFromObject(HttpServletRequest request,  
            Class<T> clazz) {  
        try {  
            return fromJSON(request.getInputStream(), clazz);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return null;  
  
    }  
  
    /**
     * json字符串转集合
     * @param jstr
     * @param li
     * @return
     */
    public static List<Object> getJsonList(String jstr, List<Object> li) {  
        char[] cstr = jstr.toCharArray();  
        boolean bend = false;  
        int istart = 0;  
        int iend = 0;  
        for (int i = 0; i < cstr.length; i++) {  
            if ((cstr[i] == '{') && !bend) {  
                istart = i;  
            }  
            if (cstr[i] == '}' && !bend) {  
                iend = i;  
                bend = true;  
            }  
        }  
  
        if (istart != 0) {  
            String substr = jstr.substring(istart, iend + 1);  
            jstr = jstr.substring(0, istart - 1)  
                    + jstr.substring(iend + 1, jstr.length());  
            substr = substr.replace(",\"children\":", "");  
            substr = substr.replace("]", "");  
            substr = substr.replace("[", "");  
            li.add(substr);  
            getJsonList(jstr, li);  
        }  
        return li;  
    }
    
} 
  